package com.free.bsf.metric.controller;

import com.free.bsf.core.base.Ref;
import com.free.bsf.core.db.DbHelper;
import com.free.bsf.core.util.StringUtils;
import com.free.bsf.metric.base.Config;
import com.free.bsf.metric.base.Pager1;
import com.free.bsf.metric.base.SpringMvcController;
import com.free.bsf.metric.dao.model.auto.tb_project_model;
import com.free.bsf.metric.dao.tb_project_dal;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Slf4j
@Controller
@RequestMapping("/project")
public class ProjectController extends SpringMvcController {
    @RequestMapping("/index")
    public ModelAndView index(String project_name, String project_desc, Integer pageindex, Integer pagesize) {
        val pageIndex2 = (pageindex == null ? 1 : pageindex);
        val pageSize2 = (pagesize == null ? 10 : pagesize);
        return pageVisit((m) -> {

            html.s2("project_name", StringUtils.nullToEmpty(project_name)).s2("project_desc", project_desc).s2("pageindex", pageindex).s2("pagesize", pagesize);
                    Ref<Integer> totalSize = new Ref<>(0);
                    val list = DbHelper.get(Config.mysqlDataSource(), c -> {
                        return new tb_project_dal().searchPage(c, project_name, project_desc, pageIndex2, pageSize2, totalSize);
                    });
                    new Pager1(pageIndex2, totalSize.getData()).setPageSize(pageSize2).out();
                    request.setAttribute("model", list);
                    val hostPort=request.getServerName()+(request.getServerPort()==80?"":":"+request.getServerPort());
                    request.setAttribute("js", "<script type=\"text/javascript\" src=\"http://"+hostPort+"/metric.js\"></script>\n" +
                            "<script>\n" +
                            "    var bsfmetric = new BsfMetric();\n" +
                            "    bsfmetric.project=\"{project_name}\";//必填\n" +
                            "    bsfmetric.userId=\"\";//用户id,可选\n" +
                            "    bsfmetric.uuId=\"\";//游客识别,可选\n" +
                            "    bsfmetric.app=\"\";//app版本,可选\n" +
                            "    bsfmetric.spa=false;//单页面浏览模式(react需配置),可选\n" +
                            "    bsfmetric.other={};//其他额外记录,可选\n" +
                            "    bsfmetric.apiWebSite=\"http://"+hostPort+"\"//api网站地址,可选\n" +
                            "    bsfmetric.init();\n" +
                            "</script>");
                }
        );
    }

    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
        html.s2("id", id);
        return pageVisit((m) -> {
            val model = DbHelper.get(Config.mysqlDataSource(), c -> {
                return new tb_project_dal().get(c, id);
            });
            val temp = new tb_project_model();
            request.setAttribute("model", model==null?temp:model);
        });
    }

    @RequestMapping("/save")
    public ModelAndView save(Integer id,String project_desc,String project_name,
                             String back_metric_config, String front_metric_config,String url
    ) {
        return jsonVisit((m) -> {
            DbHelper.call(Config.mysqlDataSource(), c -> {
                tb_project_model model = new tb_project_dal().get(c, id);
                if (model == null) {
                    model = new tb_project_model();
                    model.create_time = new Date();
                    model.update_time = new Date();
                }else {
                    model.update_time = new Date();
                }
                model.project_desc=project_desc;
                model.project_name=project_name;
                model.back_metric_config = back_metric_config;
                model.front_metric_config = front_metric_config;
                model.url=url;
                if(model.id==null||model.id==0){
                    new tb_project_dal().add(c,model);
                }else {
                    new tb_project_dal().edit(c,model);
                }
            });
            return true;
        });
    }

    @RequestMapping("/del/")
    public ModelAndView del(Integer id) {
        return jsonVisit((m) -> {
            DbHelper.call(Config.mysqlDataSource(), c -> {
                tb_project_model model = new tb_project_dal().get(c, id);
                if(model!=null) {
                    new tb_project_dal().delete(c, id);
                }
            });
            return true;
        });
    }

    @RequestMapping("/preview")
    public ModelAndView preview(Integer id) {
        html.s2("id", id);
        return pageVisit((m) -> {
            val model = DbHelper.get(Config.mysqlDataSource(), c -> {
                return new tb_project_dal().get(c, id);
            });
            val temp = new tb_project_model();
            request.setAttribute("url", model==null?temp:model.url);
        });
    }
}
